package com.yunshang.shoppingmall.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.MessageConstants;
import com.yunshang.shoppingmall.common.Response;
import com.yunshang.shoppingmall.common.layuiJson;
import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.service.ProductGuiGeService;
import com.yunshang.shoppingmall.service.ProductSalesStatisticsService;
import com.yunshang.shoppingmall.service.ProductService;
import com.yunshang.shoppingmall.service.SysUserNoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductSalesStatisticsController {
    @Autowired
    ProductSalesStatisticsService productSalesStatisticsService;

    @Autowired
    SysUserNoService sysUserNoService;

    @Autowired
    ProductService productService;

    @Autowired
    ProductGuiGeService productGuiGeService;

    @ResponseBody
    @RequestMapping("/getProductSales")
    public String getProductSales(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,@RequestParam("supplier") String supplier,@RequestParam("newpr") String newpr) {
        //System.out.println(page+"---"+limit);
        PageHelper.startPage(page,limit);
        supplier=supplier==null?"":supplier;
        newpr=newpr==null?"0":newpr;
        List<ProductSalesStatistics> productSalesStatistics=productSalesStatisticsService.selectAll(supplier,newpr);
        //System.out.println(productSalesStatistics.toString());
        PageInfo page1 = new PageInfo(productSalesStatistics);
        return layuiJson.layuiJsonList(productSalesStatistics,(int)page1.getTotal());
    }
    @ResponseBody
    @RequestMapping("/selectSupplier")
    public Map<String, Object> selectSupplier() {
        Map<String, Object> map = new HashMap<>();
        List<Product> list=productService.selectSupplier();
        System.out.println("lis:"+list.size());
        if (list != null&&list.size()>0) {
            map.put("supplier", list);
        }
        return map;
    }

    @RequestMapping("/productSalesList")
    public String productSalesList()
    {
        return "export/ProductSalesStatisticsList";
    }

    @RequestMapping("/activitySalesList")
    public String activitySalesList()
    {
        return "export/activitySalesList";
    }

    @RequestMapping("/WActivityUserNoDetails")
    public String WActivityUserNoDetails()
    {
        return "export/activitySalesOrderList";
    }

    @ResponseBody
    @RequestMapping("/activityDetails")
    public String activityDetails(@Param("uid") int uid)
    {
        List<Activity> userNoList = sysUserNoService.WActivityUserNoDetails(uid);
        return layuiJson.layuiJsonList(userNoList,userNoList.size());
    }

    @ResponseBody
    @RequestMapping("/activityList")
    public String activityList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,@Param("guige") int guige)
    {
        PageHelper.startPage(page,limit);
        List<UserNo> userNoList = sysUserNoService.ActivityList(guige);

        PageInfo page1 = new PageInfo(userNoList);
        return layuiJson.layuiJsonList(userNoList,(int)page1.getTotal());
    }

    @ResponseBody
    @RequestMapping("/activitySum")
    public String activitySum(@Param("guige") int guige)
    {
        int sum = sysUserNoService.ActivitySum(guige);
        return layuiJson.layuiJsonList(sum,sum);
    }

    @ResponseBody
    @RequestMapping("/wActivityList")
    public String wActivityList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit)
    {
        PageHelper.startPage(page,limit);
        List<UserNo> userNoList = sysUserNoService.WActivityList();

        PageInfo page1 = new PageInfo(userNoList);
        return layuiJson.layuiJsonList(userNoList,(int)page1.getTotal());
    }

    @ResponseBody
    @RequestMapping("/wActivitySum")
    public String wActivitySum()
    {
        ProductGuiGe productGuiGe=productGuiGeService.selectByPrimaryKey(505);
        int count=productGuiGe==null?0:productGuiGe.getBuyCount();
        //System.out.println(count+"ssss");
        Integer sum = sysUserNoService.WActivitySum();
        sum=sum==null?0:sum;
        return layuiJson.layuiJsonList(count,sum);
    }

//    @ResponseBody
//    @RequestMapping("/wActivitySumMother")
//    public String wActivitySumMother()
//    {
//         ProductGuiGe productGuiGe=productGuiGeService.selectByPrimaryKey(491);
//         if(productGuiGe!=null){
//             int count=productGuiGe.getBuyCount();
//             return layuiJson.layuiJsonList(count,count);
//         }else {
//             return layuiJson.layuiJsonList(0,0);
//         }
//    }

    @ResponseBody
    @RequestMapping("/activityProductList")
    public String activityProductList()
    {
        List<Product> productList = productService.selectProductByActiveSign(7);
        return layuiJson.layuiJsonList(productList,productList.size());
    }

    @ResponseBody
    @RequestMapping("/activityGuigeList")
    public String activityGuigeList(@RequestParam("productId") Integer productId)
    {
        List<ProductGuiGe> productGuiGeList = productGuiGeService.selectGuigeListByProductId(productId);
        return layuiJson.layuiJsonList(productGuiGeList,productGuiGeList.size());
    }
}
